/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2023. All rights reserved.
 */

package dao

import (
	"fmt"
	"prim-server/common"
	"prim-server/model"

	"github.com/astaxie/beego/orm"
	"github.com/pkg/errors"
	"github.com/spf13/cast"
)

type BillUpdateRecordDao struct {
	BaseDao
}

type IBillUpdateRecordDao interface {
	SaveBillUpdateRecordInfo(billUpdateRecord *model.BillUpdateRecord) error
	DeleteBillUpdateRecordInfo(userId string) error
	UpdateBillUpdateRecordInfo(billUpdateRecord *common.ModifyBillUpdateRecordDetail) error
	GetBillUpdateRecordInfo(queryParameter *common.QueryBillUpdateRecordDetail) (billUpdateRecord []*model.BillUpdateRecord, totalCount int, err error)
}

func GetBillUpdateRecordDao() IBillUpdateRecordDao {
	dao := &BillUpdateRecordDao{}
	dao.Init()
	return dao
}

func (ard *BillUpdateRecordDao) SaveBillUpdateRecordInfo(billUpdateRecord *model.BillUpdateRecord) error {
	if billUpdateRecord == nil {
		return nil
	}
	_, err := ard.O.Insert(billUpdateRecord)
	if err != nil {
		return errors.Errorf("SaveBillUpdateRecordInfo error: %v, updateRecord IndexId: %s", err, billUpdateRecord.IndexId)
	}
	return nil
}

func (ard *BillUpdateRecordDao) DeleteBillUpdateRecordInfo(userId string) error {
	if len(userId) == 0 {
		return nil
	}
	billUpdateRecord := new(model.BillUpdateRecord)
	billUpdateRecord.UserId = userId
	_, err := ard.O.Delete(billUpdateRecord, "user_id")
	if err != nil {
		return errors.Errorf("DeleteBillUpdateRecordInfo error: %v, userId: %s", err, userId)
	}
	return nil
}
func (ard *BillUpdateRecordDao) buildParameterForUpdateBillUpdateRecordInfo(billUpdateRecord *common.ModifyBillUpdateRecordDetail) (modify orm.Params) {
	modify = orm.Params{}
	if billUpdateRecord.BillUpdateOnePayCycle != nil {
		modify["bill_update_one_pay_cycle"] = *billUpdateRecord.BillUpdateOnePayCycle
	}
	if billUpdateRecord.BillUpdateOnePayReverseOffset != nil {
		modify["bill_update_one_pay_reverse_offset"] = *billUpdateRecord.BillUpdateOnePayReverseOffset
	}
	if billUpdateRecord.BillUpdateOnePayDateBegin != nil {
		modify["bill_update_one_pay_date_begin"] = *billUpdateRecord.BillUpdateOnePayDateBegin
	}
	if billUpdateRecord.BillUpdateOnDemandCycle != nil {
		modify["bill_update_on_demand_cycle"] = *billUpdateRecord.BillUpdateOnDemandCycle
	}
	if billUpdateRecord.BillUpdateOnDemandReverseOffset != nil {
		modify["bill_update_on_demand_reverse_offset"] = *billUpdateRecord.BillUpdateOnDemandReverseOffset
	}
	if billUpdateRecord.BillUpdateOnDemandDateBegin != nil {
		modify["bill_update_on_demand_date_begin"] = *billUpdateRecord.BillUpdateOnDemandDateBegin
	}
	if billUpdateRecord.ResourceUpdateOnePayCycle != nil {
		modify["resource_update_one_pay_cycle"] = *billUpdateRecord.ResourceUpdateOnePayCycle
	}
	if billUpdateRecord.ResourceUpdateOnePayReverseOffset != nil {
		modify["resource_update_one_pay_reverse_offset"] = *billUpdateRecord.ResourceUpdateOnePayReverseOffset
	}
	if billUpdateRecord.ResourceUpdateOnePayDateBegin != nil {
		modify["resource_update_one_pay_date_begin"] = *billUpdateRecord.ResourceUpdateOnePayDateBegin
	}
	if billUpdateRecord.ResourceUpdateOnDemandCycle != nil {
		modify["resource_update_on_demand_cycle"] = *billUpdateRecord.ResourceUpdateOnDemandCycle
	}
	if billUpdateRecord.ResourceUpdateOnDemandReverseOffset != nil {
		modify["resource_update_on_demand_reverse_offset"] = *billUpdateRecord.ResourceUpdateOnDemandReverseOffset
	}
	if billUpdateRecord.ResourceUpdateOnDemandDateBegin != nil {
		modify["resource_update_on_demand_date_begin"] = *billUpdateRecord.ResourceUpdateOnDemandDateBegin
	}
	return modify
}

func (ard *BillUpdateRecordDao) UpdateBillUpdateRecordInfo(billUpdateRecord *common.ModifyBillUpdateRecordDetail) error {
	if billUpdateRecord == nil {
		return nil
	}
	modify := ard.buildParameterForUpdateBillUpdateRecordInfo(billUpdateRecord)
	if len(modify) != 0 {
		_, err := ard.O.QueryTable(&model.BillUpdateRecord{}).Filter("user_id", billUpdateRecord.UserId).Update(modify)
		if err != nil {
			return errors.Errorf("UpdateBillUpdateRecordInfo error: %v, userId: %s", err, billUpdateRecord.UserId)
		}
	}
	return nil
}

func (ard *BillUpdateRecordDao) GetBillUpdateRecordInfo(queryParameter *common.QueryBillUpdateRecordDetail) (billUpdateRecord []*model.BillUpdateRecord, totalCount int, err error) {
	if queryParameter == nil {
		return nil, 0, nil
	}
	recode := ard.O.QueryTable(&model.BillUpdateRecord{})
	finalCondition := orm.NewCondition()
	if len(queryParameter.UserId) > 0 {
		finalCondition = finalCondition.And("user_id__in", queryParameter.UserId)
	}
	offset, pageSize := ard.GetOffsetAndPageSize(nil, nil)
	recode = recode.SetCond(finalCondition)
	totalCount64, err := recode.Count()
	if err != nil {
		return nil, 0, errors.Errorf("GetBillUpdateRecordInfo error: %v, userId: %s", err, fmt.Sprint(queryParameter.UserId))
	}
	totalCount = cast.ToInt(totalCount64)
	_, err = recode.Offset(offset).Limit(pageSize).All(&billUpdateRecord)
	if err != nil {
		if err == orm.ErrNoRows {
			//not found
			return nil, 0, nil
		}
		//found error
		return nil, 0, errors.Errorf("GetBillUpdateRecordInfo error: %v, userId: %s", err, fmt.Sprint(queryParameter.UserId))
	}
	return billUpdateRecord, totalCount, nil
}
